package com.lfz.quality_effect.controller;


import com.lfz.quality_effect.exception.MyException;
import com.lfz.quality_effect.model.dto.TimeDTO;
import com.lfz.quality_effect.model.vo.*;
import com.lfz.quality_effect.service.TIndexScoreService;
import com.lfz.quality_effect.utils.ResultData;
import com.lfz.quality_effect.utils.ReturnCode;
import com.lfz.quality_effect.utils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 指标得分表 前端控制器
 * </p>
 *
 * @author Destiny
 * @since 2023-03-03
 */
@Api(tags = "指标得分",value = "TCollectionBalanceController")
@RestController
@RequestMapping("/api/indexScore")
public class TIndexScoreController {

    @Autowired
    private TIndexScoreService indexScoreService;
    /**
     * 上传指标得分明细
     */
    @ApiOperation("上传指标得分")
    @PostMapping(value = "/uploadIndexItemScore")
    public ResultData uploadIndexItemScore(@RequestParam("file") MultipartFile file,String court,String dataTime){
        if (StringUtils.isEmpty(court)){
            throw new MyException(ReturnCode.RC10000.getCode(),ReturnCode.RC10000.getMsg());
        }
        if (StringUtils.isEmpty(dataTime)){
            throw new MyException(ReturnCode.RC10001.getCode(),ReturnCode.RC10001.getMsg());
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dataTime = simpleDateFormat.format(TimeUtils.vertifyTime(dataTime));

        }catch (Exception e){
            throw new MyException(ReturnCode.RC10002.getCode(),ReturnCode.RC10002.getMsg());
        }
        indexScoreService.uploadIndexItemScore(file,court,dataTime);
        return ResultData.ok();
    }

    /**
     * 获取总分排名
     * @return
     */
    @ApiOperation("获取总分排名")
    @PostMapping(value = "/getScoreRank")
    public ResultData<List<ScoreRankVO>> getScoreRank(){
        List<ScoreRankVO> scoreRankVOList = indexScoreService.getScoreRank();
        return ResultData.ok().data(scoreRankVOList);
    }

    /**
     * 非满分指标情况
     */
    @ApiOperation("非满分指标情况")
    @PostMapping("/getNonPerfectScoreInfo")
    public ResultData getNonPerfectScoreInfo(){
        NonPerfectIndexVO  nonPerfectIndexVO = indexScoreService.getNonPerfectScoreInfo();
        return ResultData.ok().data(nonPerfectIndexVO);
    }

    @ApiOperation("获取速裁快审指标")
    @PostMapping("/getJudgeQuicklyScoreInfo")
    public ResultData getJudgeQuicklyScoreInfo(){
        String name  = "速裁、快审案件平均审理期限";

        ResultData resultData = indexScoreService.getJudgeQuicklyScoreInfo(name);
        return resultData;
    }
    @ApiOperation("获取电子送达率指标")
    @PostMapping("/getElectronicDeliveryScoreInfo")
    public ResultData getElectronicDeliveryScoreInfo(){
        String name = "电子送达率";
        ResultData resultData = indexScoreService.getElectronicDeliveryScoreInfo(name);
        return resultData;
    }

    @ApiOperation("获取指定法院的预警信息")
    @PostMapping("/getEarnWarningInfo")
    public ResultData getEarnWarningInfo(){
        String court = "乐平市人民法院";
        List<EarnWarnVO> earnWarnVOList = indexScoreService.getEarnWarningInfo(court);
        return ResultData.ok().data(earnWarnVOList);
    }

    @ApiOperation("电子送达趋势图")
    @PostMapping("/electronicDeliveryTrand")
    public ResultData electronicDeliveryTrand(@RequestBody TimeDTO timeDTO){

        String name = "电子送达率";

        String endTime = timeDTO.getEndTime();
        String startTime = timeDTO.getStartTime();
        Map<String, List<EleScoreRankVO>> indexScoreVOList = indexScoreService.electronicDeliveryTrand(name,startTime,endTime);
        return ResultData.ok().data(indexScoreVOList);
    }

    @ApiOperation("速裁快审趋势图")
    @PostMapping("/quicklyJudgeTrend")
    public ResultData quicklyJudgeTrend(@RequestBody TimeDTO timeDTO){

        String name  = "速裁、快审案件平均审理期限";

        String endTime = timeDTO.getEndTime();
        String startTime = timeDTO.getStartTime();
        Map<String, List<EleScoreRankVO>> indexScoreVOList = indexScoreService.quicklyJudgeTrend(name,startTime,endTime);
        return ResultData.ok().data(indexScoreVOList);
    }

}

